"""
删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
"""


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

    def __repr__(self):
        r = []
        node = self
        while node:
            r.append(str(node.val))
            node = node.next
        return "->".join(r)


class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        tmp = head
        head = ListNode(None)
        head.next = tmp
        tmp = head
        while tmp and tmp.next:
            if tmp.next.val == val:
                tmp.next = tmp.next.next
            else:
                tmp = tmp.next
        return head.next


if __name__ == '__main__':
    arr = [1, 2, 2, 1, 1]
    p1 = p2 = ListNode(None)
    for i in arr:
        p1.next = ListNode(i)
        p1 = p1.next
    print(p2.next)
    s = Solution()
    print(s.removeElements(p2.next, 1))

